﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SceneFrame
{
    public class FilterTime : IFilter
    {
        public const string NEXT_RUN_TIME_KEY = "NextRunTime";

        /// <summary>
        /// 下次运行时间
        /// </summary>
        protected long LastRunTime
        {
            get
            {
                var context = Scene.GetContextData();
                var time = context.ContainsKey(NEXT_RUN_TIME_KEY) ? context[NEXT_RUN_TIME_KEY] : SceneManager.TimeNow.ToString();
                return long.Parse(time);
            }
        }

        /// <summary>
        /// 时间间隔
        /// </summary>
        public long Interval { get; set; }

        public override void UpdateContextData(Dictionary<string, string> data)
        {
            base.UpdateContextData(data);

            // 更新下次运行的事件
            data[NEXT_RUN_TIME_KEY] = (SceneManager.TimeNow + Interval).ToString();
        }

        public override bool Filter(Trigger trigger)
        {
            return SceneManager.TimeNow >= LastRunTime;
        }
    }
}
